//
//  QuartzFunView.m
//  QuartzFun
//
//  Created by KaiKai on 11-8-22.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "QuartzFunView.h"
#import "UIColor-Random.h"

@implementation QuartzFunView
@synthesize firstTouch;
@synthesize lastTouch;
@synthesize currentColor;
@synthesize shapeType;
@synthesize drawImage;
@synthesize useRandomColor;	
@synthesize redrawRect;
@dynamic currentRect;


- (id)initWithCoder:(NSCoder *)aDecoder
{
	if ((self = [super initWithCoder:aDecoder]))
	{
		self.currentColor = [UIColor redColor];
		self.useRandomColor = NO;
		if (drawImage == nil)
		{
			self.drawImage = [UIImage imageNamed:@"景甜.png"];
		}
	}
	
	return self;
}

- (CGRect)currentRect
{
	return CGRectMake((firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x, (firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y, fabsf(firstTouch.x - lastTouch.x), fabsf(firstTouch.y - lastTouch.y));	
}

- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetLineWidth(context, 2.0);
	CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
	CGContextSetFillColorWithColor(context, currentColor.CGColor);
	
	
	switch (shapeType)
	{
		case KLineShape:
			CGContextMoveToPoint(context, firstTouch.x, firstTouch.y);
			CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y);
			CGContextStrokePath(context);
			break;
			
		case KRectShape:
			CGContextAddRect(context, self.currentRect);
			CGContextDrawPath(context, kCGPathFillStroke);
//			CGContextStrokePath(context);
			break;
			
		case KEllipseShape:
			CGContextAddEllipseInRect(context, self.currentRect);
			CGContextDrawPath(context, kCGPathFillStroke);
//			CGContextStrokePath(context);
			break;
			
		case KImageShape:
		{
			CGFloat horizontalOffset = drawImage.size.width / 2;
			CGFloat verticalOffset = drawImage.size.height / 2;
			CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset);
			[drawImage drawAtPoint:drawPoint];
			
			break;
		}
			
		default:
			break;
	}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (useRandomColor)
		self.currentColor = [UIColor randomColor];
	
	UITouch *touch = [touches anyObject];
	firstTouch = [touch locationInView:self];
	lastTouch = [touch locationInView:self];
	
	if (shapeType == KImageShape)
	{
		CGFloat horizontalOffset = drawImage.size.width / 2;
		CGFloat verticalOffset = drawImage.size.height / 2;
		redrawRect = CGRectMake(firstTouch.x - horizontalOffset, firstTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height);
	}
	else
	{
		redrawRect = CGRectMake(firstTouch.x, firstTouch.y, 0, 0);
	}
	
	[self setNeedsDisplay];
//	[self setNeedsDisplayInRect:redrawRect];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	lastTouch = [touch locationInView:self];

	if (shapeType == KImageShape)
	{
		CGFloat horizontalOffset = drawImage.size.width / 2;
		CGFloat verticalOffset = drawImage.size.height / 2;
		redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
	}
	else
	{
		redrawRect = CGRectUnion(redrawRect, self.currentRect);
	}
	
	[self setNeedsDisplayInRect:redrawRect];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	lastTouch = [touch locationInView:self];
	
	if (shapeType == KImageShape)
	{
		CGFloat horizontalOffset = drawImage.size.width / 2;
		CGFloat verticalOffset = drawImage.size.height / 2;
		redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
	}
	else
	{
		redrawRect = CGRectUnion(redrawRect, self.currentRect);
	}
	
	[self setNeedsDisplayInRect:redrawRect];
}

- (void)dealloc
{
	[currentColor release];
	[drawImage release];
	
	[super dealloc];
}

@end
